# -*- coding: utf-8 -*-
# @Author: liuzhen
# @Date:   2018-07-26 15:58:03
# @Last Modified by:   Sparks_lz
# @Last Modified time: 2018-08-14 11:30:51
import os
import sys
import maya.standalone as standalone
standalone.initialize()
import maya.cmds as cmds
import pymel.core as pmc
import platform


X = os.sep

cmds.loadPlugin('AbcBullet')
cmds.loadPlugin('AbcExport')
cmds.loadPlugin('AbcImport')

mayafile = sys.argv[1]

if platform.system() == 'Windows' :  
    filepath = "\\".join(mayafile.split("/"))
else :  
    filepath = mayafile

scenename = pmc.sceneName()
dirname = scenename.dirname()


cmds.file(filepath, f=1 , o=1, type='mayaBinary', ignoreVersion=1 , options='v=0;')

start = int(pmc.playbackOptions(q=1,min=1))
end = int(pmc.playbackOptions(q=1,max=1))

print start
print end



def exportCamera(node, basepath, start, end) :
    origin_cam = node
    print type(origin_cam)
    # trans_cam = origin_cam.listRelatives(type="transform")[0]
    camera_attr = ["translateX",
                   "translateY",
                   "translateZ",
                   "rotateX",
                   "rotateY",
                   "rotateZ",
                   "scaleX",
                   "scaleY",
                   "scaleZ"]
    for i in camera_attr:
        camAttr = pmc.Attribute("%s.%s" % (origin_cam.name(), i))
        camAttr.showInChannelBox(True)
        if camAttr.isLocked():
            camAttr.unlock()
        else :
            pass
    new_cam = pmc.duplicate(origin_cam, rr=True)[0]
    try :
        pmc.parent(new_cam, world=True)
    except :
        pass
    new_camera_pc = pmc.parentConstraint(origin_cam, new_cam, mo=True, weight=1)
    for attr in camera_attr :
        pmc.bakeResults("%s.%s" % (new_cam.name(), i), t=(start, end), simulation=True, sb=1)

    print "@@@@@@@@@@@@@@@@@@@@@@@@@@"




    print new_camera_pc
    # pmc.bakeSimulation(new_cam, t=(start,end), sb=1, pok=1, sac=0)
    # pmc.bakeResults(new_cam, t=(start,end), simulation=True)
    print "@@@@@@@@@@@@@@@@@@@@@@@@@@@"
    # pmc.delete(new_camera_pc)


    root = "-root %s " % (new_cam.longName())
    output_dir = basepath + X + "Camera"
    nnnname = str(pmc.sceneName().basename().split("_")[0])
    print "################"
    print nnnname
    output_path = output_dir + X + new_cam.name() + ".abc"
    # command = "-frameRange " + str(start) + " " + str(end) + " "  + root + " -dataFormat ogawa -file " + output_path
    command = "-frameRange " + str(start) + " " + str(end) + " "  + root + " -dataFormat ogawa -file " + output_path

    print "@@@command@@@"
    print command
    print "@@@@@@@@@@"
    filepath = os.path.dirname(output_path)
    if os.path.exists(filepath) :
        cmds.AbcExport ( j = command )

    else :
        print output_path
        os.makedirs(filepath)
        # os.makedirs(output_dir)
        cmds.AbcExport ( j = command )
    # pmc.delete(new_cam)
    print "Done directory : %s" % (output_path)
    pmc.select(new_cam)
    # cmds.file( rename='%s'%(new_cam.name() ))
    ma_export_path = os.path.join(output_dir, "%s.mb"%(new_cam))
    cmds.file( str(ma_export_path),
               force=True, 
               exportSelected=True, 
               type='mayaBinary',  
               options='v=0;')




blacklist = ["frontShape", "perspShape", "sideShape", "topShape"]
cam_list = [cam for cam in pmc.ls(type="camera") if cam not in blacklist]

for c in cam_list :
    trans = c.getTransform()
    camera_attr = ["translateX",
                   "translateY",
                   "translateZ",
                   "rotateX",
                   "rotateY",
                   "rotateZ",
                   "scaleX",
                   "scaleY",
                   "scaleZ"]
    for attr in camera_attr :
        print pmc.Attribute("%s.%s" % (trans, attr)).get()
    print trans.type()
    print os.path.dirname(filepath)
    print start,"~",end
    exportCamera(trans, os.path.dirname(filepath), start, end)




print "everything is done"

# cmds.file("C:\\Users\\liuzhen\\Desktop\\abc\\llll.mb", f=1 , exportAll=1, type='mayaBinary', ignoreVersion=1 , options='v=0;')

standalone.uninitialize()
